package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Users;
import com.bwie.common.domain.request.UserRequest;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.remote.UserRemote;
import com.bwie.service.AuthService;
import com.bwie.sms.SendSmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserRemote userRemote;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SendSmsUtils sendSmsUtils;

    @Override
    public void phone(String tel) {
        //手机号判空
        if(StrUtil.isBlank(tel)){
            throw new RuntimeException("手机号不能为空");
        }
        //手机号格式校验
        if(!tel.matches("1[3-9][0-9]{9,}")){
            throw new RuntimeException("手机号格式不正确");
        }

        //远程调用
        Result<Users> result = userRemote.login(tel);
//        //熔断
//        if(result.getCode()==Result.FAIL){
//            throw new RuntimeException("3......");
//        }
        Users data = result.getData();
        if(data==null){
            throw new RuntimeException("不能为空");
        }

        //发送验证码
        Random random = new Random();
        int i = random.nextInt(9999 - 1000 + 1) + 1000;
        //将验证码存入redis中
        redisTemplate.opsForValue().set("tel"+tel,i+"",15, TimeUnit.MINUTES);
        sendSmsUtils.sendSms(tel,i+"");
        System.out.println(i+"");
    }

    @Override
    public UserRequest login(Users user) {
        //手机号和验证码校验
        String tel = user.getTel();
        String code = user.getCode();
        if(StrUtil.isBlank(code)){
            throw new RuntimeException("验证码不能为空");
        }
        if(StrUtil.isBlank(tel)){
            throw new RuntimeException("手机号不能为空");
        }

        //验证码校验 先从redis 中获取验证码
        String object = (String) redisTemplate.opsForValue().get("tel" + tel);
        if(!code.equals(object)){
            throw new RuntimeException("验证码错误");
        }

        //远程调用
        Result<Users> result = userRemote.login(tel);
        Users data = result.getData();
        if (data==null){
            throw new RuntimeException("手机号已存在");
        }

        Map<String, Object> map = new HashMap<>();
        String userKey = UuidUtils.generateUuid();
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey,data,15,TimeUnit.MINUTES);
        UserRequest request = new UserRequest();
        request.setToken(token);
        request.setUserTime("30MIN");
        return request;
    }
}
